package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.WqEweiShopMemberAddress;
import com.ruoyi.sj.mapper.WqEweiShopMemberAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ShippingAddressService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/10 0010 11:40
 **/
@Service
public class AppShippingAddressService {
    @Autowired
    private WqEweiShopMemberAddressMapper addressMapper;

    /**
     * 新增收货地址
     * @param map
     * @return
     */
    public Object addAddress(Map<String, String> map) {
        WqEweiShopMemberAddress address = new WqEweiShopMemberAddress();
        if (StringUtils.isBlank(map.get("memberId"))||"null".equals(map.get("memberId"))){
            throw new XException("用户id不能为空");
        }
        address.setOpenid(map.get("memberId")); // 用户ID
        address.setAddress(map.get("address")); //具体地址
        address.setRealname(map.get("realname")); //收货人
        address.setMobile(map.get("mobile")); //电话
        address.setProvince(map.get("province")); //省
        address.setCity(map.get("city")); //市
        address.setArea(map.get("area")); //区
        address.setIsdefault(2); //表示不默认
        return addressMapper.insertWqEweiShopMemberAddress(address);
    }

    /**
     * 编辑收货地址
     * @param map
     * @return
     */
    public Object updateAddress(Map<String, String> map) {
        WqEweiShopMemberAddress address = new WqEweiShopMemberAddress();
        if (StringUtils.isBlank(map.get("id"))||"null".equals(map.get("id"))){
            throw new XException("id不能为空");
        }
        address.setId(Integer.valueOf(map.get("id")));
        address.setAddress(map.get("address")); //具体地址
        address.setRealname(map.get("realname")); //收货人
        address.setMobile(map.get("mobile")); //电话
        address.setProvince(map.get("province")); //省
        address.setCity(map.get("city")); //市
        address.setArea(map.get("area")); //区
        return addressMapper.updateWqEweiShopMemberAddress(address);
    }

    /**
     * 收货地址列表
     * @return
     */
    public List<WqEweiShopMemberAddress> queryAddress(String memberId) {
        return addressMapper.selectByMemberId(memberId);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    public Object deleteAddress(String id) {
        if (StringUtils.isBlank(id)){
            throw new XException("id不能为空");
        }
        return addressMapper.deleteWqEweiShopMemberAddressById(Integer.valueOf(id));
    }

    /**
     * 更改状态
     * @param id
     * @return
     */
    public Object updateStatus(String id,String memberId) {
        if (StringUtils.isBlank(id)){
            throw new XException("id不能为空");
        }
        addressMapper.updateStatus(memberId);//将所有状态都初始化为2
        int updateById = addressMapper.updateById(id);//修改单条数据状态
        return updateById;
    }
}
